package org.uni_hi.lsep.gui.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.uni_hi.lsep.AppManager;
import org.uni_hi.lsep.gui.MainFrame;
import org.uni_hi.lsep.gui.util.Config;
import org.uni_hi.lsep.text.ChapterNode;
import org.uni_hi.lsep.text.ChapterNode.NodeType;
import org.w3c.dom.Node;

public class QuizAnswerPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton submitButton;
	private ImageIcon icon_right;
	private ImageIcon icon_wrong;
	private ButtonGroup answerButtonGroup;
	private int clickCount=0;
	private int[] correctCount=new int[3];

	public QuizAnswerPanel(){
		init();
	}

	private void init(){
		BoxLayout bl=new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(bl);
		this.setAlignmentX(CENTER_ALIGNMENT);
		answerButtonGroup=new ButtonGroup();
	}

	/**
	 * This method initializes submitButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSubmitButton() {
		if (submitButton == null) {
			submitButton = new JButton();
			submitButton.setName("submitButton");
			submitButton.setText("OK");
			submitButton.setAlignmentX(CENTER_ALIGNMENT);
			submitButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					int correctAnswers=0;
					if(arg0.getActionCommand().equals("OK")){
						Enumeration<AbstractButton>en=answerButtonGroup.getElements();
						JRadioButton ab=null;
						while(en.hasMoreElements()){
							ab=(JRadioButton)en.nextElement();
							if(ab.isSelected()) break;
						}
						if(ab!=null && answerButtonGroup.getSelection()!=null){
							if(answerButtonGroup.getSelection().getActionCommand()!=null){
								//richtige Antwort
								getSubmitButton().setText("Weiter");
								en=answerButtonGroup.getElements();
								while(en.hasMoreElements()){
									ab=(JRadioButton)en.nextElement();
									if(null==ab.getModel().getActionCommand()){
										ab.setIcon(getWrongIcon());
									}else{
										ab.setIcon(getRightIcon());
									}
								}
								correctAnswers++;
							}else{
								//falsche Antwort
								ab.setIcon(getWrongIcon());
							}
						}
						if(clickCount<correctCount.length){
							correctCount[clickCount]+=correctAnswers;
						}else{
							correctCount[correctCount.length-1]+=correctAnswers;
						}
						clickCount++;
					}else{
						//Let's get it on to the next test!!
						DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)AppManager.getMainFrame().getIndexTree().getSelectionPath().getLastPathComponent();
						dmtn=dmtn.getNextNode();
						
						AppManager.getMainFrame().setStatus(" ");
						getSubmitButton().setText("OK");
						if(((ChapterNode)dmtn.getUserObject()).getNodeType().equals(NodeType.QUIZ)){
							AppManager.getMainFrame().getIndexTree().setSelectionPath(new TreePath(dmtn.getPath()));
						}else{
							AppManager.getMainFrame().setContentPanelType(MainFrame.ContentPanelType.RESULT);
							AppManager.getMainFrame().getTestPanel().addResultPanel(correctCount);
							correctCount=new int[3];
						}
					}
				}
			});
		}
		return submitButton;
	}

	private ImageIcon getRightIcon(){
		if(icon_right==null){
			icon_right=new ImageIcon(Config.imgSrc+Config.iconRightSrc);
		}
		return icon_right;
	}

	private ImageIcon getWrongIcon(){
		if(icon_wrong==null){
			icon_wrong=new ImageIcon(Config.imgSrc+Config.iconWrongSrc);
		}
		return icon_wrong;
	}

	public void removeAnswers(){
		this.removeAll();
		Enumeration<AbstractButton> en=answerButtonGroup.getElements();
		while(en.hasMoreElements()){
			answerButtonGroup.remove(en.nextElement());
		}
		clickCount=0;
		getSubmitButton().setText("OK");
		this.add(getSubmitButton());
	}

	public void addAnswer(Node n){
		Node tn=n.getAttributes().getNamedItem("correct");
		String s= tn==null?null:tn.getNodeValue();
		this.remove(getSubmitButton());
		JRadioButton jrb=new JRadioButton(n.getTextContent());
		jrb.setActionCommand(s);
		answerButtonGroup.add(jrb);
		jrb.setAlignmentX(LEFT_ALIGNMENT);
		this.add(jrb);
		this.add(getSubmitButton());
	}
}
